package com.globalwave.common.web;

import java.util.Map;

import com.globalwave.base.BaseAction;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.entity.EventLog;
import com.globalwave.common.entity.VirtualEditForm;
import com.globalwave.common.entity.VirtualGrid;
import com.globalwave.common.service.EventLogService;
import com.globalwave.common.service.VirtualModuleService;
import com.globalwave.system.entity.SessionUser;
import com.opensymphony.xwork2.Preparable;

public class VirtualModuleAction extends BaseAction implements Preparable {

    private static final long serialVersionUID = 8582051849254108462L;
    
    private VirtualModuleService virtualModuleService ;
    private EventLogService eventLogService ;
    
    private Long view_id ;
    protected String view_code ;
    
    private Map<String, String[]> entity ;
    private Map<String, String[]> criterion ;
    
    public String execute() throws Exception {    
        return this.list();
    }


    public String getUIMetaData() throws Exception {   
    	VirtualGrid grid = virtualModuleService.getUIMetaData(view_id, view_code) ;

        renderXML(grid.asXML()) ;
        
        return null;
    }


    public String getEditFormMetaData() throws Exception {   
    	VirtualEditForm form = virtualModuleService.getEditFormMetaData(view_code) ;

        renderXML(form.asXML()) ;
        
        return null;
    }

    public String list() throws Exception {      
    	
        log("VM0000001", this.getSessionUser(), view_id, null) ;
        
        ArrayPageList<Map<String,Object>> pageList = 
        	virtualModuleService.query(criterion,pageIndex,pageSize,null,view_id, view_code, this.getSessionUser()) ;

        renderXML(pageList.asXML()) ;
        
        return null ;    
        
    }
    
    public String create()  throws Exception {
    	
        log("VM0000002", this.getSessionUser(), view_id, entity) ;
    	
        Object newFile = virtualModuleService.create(entity, view_id, this.getSessionUser()) ;

        renderXML(this.getSuccessCreateMessage(newFile).asXML()) ;
        
        return null;    
        
    }
    
    public String update()  throws Exception {     
    	
        log("VM0000003", this.getSessionUser(), view_id, entity) ;

        virtualModuleService.update(entity, view_id, this.getSessionUser()) ;

        renderXML(this.getSuccessUpdateMessage(entity).asXML()) ;
        
        return null;    
        
    }
    
    public String delete()  throws Exception {
    	/*
        if (this.ids == null) { 
            virtualModuleService.delete(entity) ;
        } else {
            virtualModuleService.deleteAll(this.ids) ;
        }*/
    	
        log("VM0000004", this.getSessionUser(), view_id, entity) ;

    	virtualModuleService.deleteAll(this.getRequest().getParameter("params"), view_id, this.getSessionUser()) ;

        renderXML(this.getSuccessDeleteMessage().asXML()) ;
        
        return null;    
        
    }

    private void log(String eventId, SessionUser user, Long viewId, Map<String, String[]> entity) {
    	
    	final EventLog eventLog = new EventLog() ;
    	  
    	eventLog.setEvent_type_code(eventId) ;
    	if (user != null) {
	      	eventLog.setUser_id(String.valueOf(user.getUser().getUser_id())) ;
	    	eventLog.setUser_name(user.getUser().getName_()) ;
    	}
    	eventLog.setBiz_id(String.valueOf(viewId)) ;

    	if (entity != null) {
			String result = "{" ;
			for (Object key:entity.keySet()) {
				String[] values = entity.get(key) ;
				String value = null ;
				if (values != null && values.length != 0) {
					if (values.length == 1) {
						value = values[0] ;
					} else {
						value = "{" ;
						for (String str:entity.get(key)) {
							value += str + ",";
						}
						value += "}" ;
					}
				}
				result += key + "=" + value + ", ";
			}
			
			if (result.length() > 2490) {
				result = result.substring(2490) + " ..." ;
			}
	    	eventLog.setBiz_data(result + "}") ;
    	}
    	
    	try {
    	    this.eventLogService.create(eventLog);
    	}catch(Exception e) {
    		log.error(e, e) ;
    	}	
    }
    
    public void prepare() throws Exception {
        // TODO Auto-generated method stub
        
    }


	public Long getView_id() {
		return view_id;
	}


	public void setView_id(Long view_id) {
		this.view_id = view_id;
	}


	public Map<String, String[]> getEntity() {
		return entity;
	}


	public void setEntity(Map<String, String[]> entity) {
		this.entity = entity;
	}


	public Map<String, String[]> getCriterion() {
		return criterion;
	}


	public void setCriterion(Map<String, String[]> criterion) {
		this.criterion = criterion;
	}


	public void setVirtualModuleService(VirtualModuleService virtualModuleService) {
		this.virtualModuleService = virtualModuleService;
	}


	public void setEventLogService(EventLogService eventLogService) {
		this.eventLogService = eventLogService;
	}


	public String getView_code() {
		return view_code;
	}


	public void setView_code(String view_code) {
		this.view_code = view_code;
	}
    
}
